import 'package:lib_database/lib_database.dart';
import 'package:lib_network/http_config.dart';
import 'package:lib_network/http_manager.dart';

import '../../../module_common/network/http_api.dart';
import 'integral_model.dart';
class TaskListInterface{
  List<TaskInfoType> task_list;
  String? custom_uid;

  TaskListInterface({
    required this.task_list,
    this.custom_uid = null,
  });
}
class IntegralApi {

  // 获取有效积分消息数量
  static Future<String?> getScoreMsgCount(String type) async {
    try {
      var resp = await HttpManager().post(API.getScoreMsgCount, {
        "chat_type": type //  会话类型 1-单聊 2-群聊
      });
      print('resp!.data${type} : ${resp!.data}');
      if (resp.code == HttpConfig().successCode) {
        // 0  或者 1
        return '  (${resp.data['count']}/${resp.data['daily_limit']})';
      }
      return null;
    } catch (e) {
      return null;
    }
  }

  // 获取任务任务完成状态
  static Future<String?> getTaskIsFinishStatus(TaskType type) async {
    try {
      var resp = await HttpManager().post(API.getTaskList, {
        "task_type": type.index // 任务类型 1签到 2新手任务 3日常任务 4进阶任务
      });
      print('resp!.data${type} : ${resp!.data}');
      if (resp.code == HttpConfig().successCode) {
        var model = await TaskListResponse.fromJson(resp.data);
        // 0  或者 1
        return model.task_status.toString();
      }
      return null;
    } catch (e) {
      return null;
    }
  }
  // 获取任务列表
  static Future<TaskListInterface> getTaskList(TaskType type) async {
    try {
      var resp = await HttpManager().post(API.getTaskList, {
        "task_type": type.index // 任务类型 1签到 2新手任务 3日常任务 4进阶任务
      });
      print('resp!.data${type} : ${resp!.data}');
      if (resp.code == HttpConfig().successCode) {
        var model = await TaskListResponse.fromJson(resp.data);
        return TaskListInterface(task_list: model.task_list, custom_uid: model.custom_uid);
      }
      throw Exception('failed');
    } catch (e) {
      throw Exception('failed: $e');
      // return TaskListInterface(task_list: [], custom_uid: model.custom_uid);
    }
  }

  // 获取用户积分
  static Future<int> getUnclaimedScore() async {
    try {
      var resp = await HttpManager().post(API.getUnclaimedScore, {
        // "task_type": TaskType.none.index // 任务类型 1签到 2新手任务 3日常任务 4进阶任务
      });
      print('resp!.data ${resp!.data}');
      if (resp.code == HttpConfig().successCode) {
        var model = TaskScoreResponse.fromJson(resp.data);
        print('getUnclaimedScore $model');
        return int.parse(model.score ?? '-1');
      }
      return -1;
    } catch (e) {
      print('failed: $e');
      return -1;
    }
  }

  // 领取积分
  static Future<bool> postClaimIntegral({required String taskId}) async {
    try {
      print('{"task_id": taskId.toString()}  ${{"task_id": taskId.toString()}}');
      var resp = await HttpManager().post(API.postClaimIntegral, {"task_id": taskId.toString()});
      print('resp!.data ${resp!.data}');
      if (resp.code == HttpConfig().successCode) {
        return true;
      }
      return false;
    } catch (e) {
      print('failed: $e');
      return false;
    }
  }

  // 批量领取积分
  static Future<bool> postMultiClaimIntegral({required TaskType taskType}) async {
    try {
      var resp = await HttpManager().post(API.postMultiClaimIntegral, {
        "task_type": taskType.index // 任务类型 1签到 2新手任务 3日常任务 4进阶任务
      });
      print('postMultiClaimIntegral data: ${resp!.data}');
      if (resp.code == HttpConfig().successCode) {
        return true;
      }
      return false;
    } catch (e) {
      return false;
    }
  }

  // 任务状态上报
  static Future<bool> getUserTaskUpdate({required TaskType taskType, required int taskCode}) async {
    try {
      var resp = await HttpManager().post(API.getUserTaskUpdate, {
        "task_type": taskType.index, // 任务类型 1签到 2新手任务 3日常任务 4进阶任务
        "task_code": taskCode,
      });
      print('resp!.data ${resp!.data}');
      if (resp.code == HttpConfig().successCode) {
        return true;
      }
      return false;
    } catch (e) {
      return false;
    }
  }

  // 用户签到
  static Future<int?> postUserSignIn() async {
    try {
      var resp = await HttpManager().post(API.postUserSignIn, {});
      print('resp!.data ${resp!.data}');
      if (resp.code == HttpConfig().successCode) {
        // 任务类型 -1签到失败 0已签到 1签到成功
        var model = TaskSignResponse.fromJson(resp.data);
        return model.status;
      }
      return null;
    } catch (e) {
      print('批量领取积分 失败: $e');
      return null;
    }
  }

  // 获取用户签到记录
  static Future<List<TaskSignDateItem>?> getSignDate({String? historyDate = null}) async {
    try {
      Map<String, dynamic> params = {};
      if (historyDate != '') {
        params['his_date'] = historyDate;
      }
      var resp = await HttpManager().post(API.getSignDate, params);
      print('获取用户签到记录 ${resp}');
      print('获取用户签到记录 ${resp!.data}');
      if (resp.code == HttpConfig().successCode) {
        // 任务类型 -1签到失败 0已签到 1签到成功
        var model = TaskSignDateResponse.fromJson(resp.data);
        return model.sign_info;
      }
      return null;
    } catch (e) {
      print('获取用户签到记录 失败: $e');
      return null;
    }
  }

  // 获取用户签到记录
  static Future<List<TaskSignDateItem>> getSignDate2({String historyDate = ''}) async {
    try {
      Map<String, dynamic> params = {};
      if (historyDate != '') {
        params['his_date'] = historyDate;
      }
      var resp = await HttpManager().post(API.getSignDate, params);
      if (resp!.code == HttpConfig().successCode) {
        // 任务类型 -1签到失败 0已签到 1签到成功
        var model = TaskSignDateResponse.fromJson(resp.data);
        return model.sign_info;
      }
      return [];
    } catch (e) {
      print('获取用户签到记录 失败: $e');
      return [];
    }
  }

  // 获取用户积分记录
  static Future<Map<String, dynamic>> getIntegralRecord({int? page = 1, int? size = 15}) async {
    try {
      Map<String, dynamic> params = {'page': page, 'size': size};
      var resp = await HttpManager().post(API.getIntegralRecord, params);
      print('resp!.data ${resp!.data}');
      if (resp.code == HttpConfig().successCode) {
        // 任务类型 -1签到失败 0已签到 1签到成功
        return resp.data;
      }
      return {"count": 0, "record_list": []};
    } catch (e) {
      print('获取用户积分记录 失败: $e');
      throw Exception(e.toString());
    }
  }

  // 获取用户积分记录
  static Future<IntegralRankResponse?> getIntegralRank({int? page = 1, int? size = 15}) async {
    try {
      Map<String, dynamic> params = {'page': page};
      var resp = await HttpManager().post(API.getIntegralRank, params);
      print('getIntegralRank : ${resp!.data}');
      if (resp.code == HttpConfig().successCode) {
        return IntegralRankResponse.fromJson(resp.data);
      }
      throw Exception('getIntegralRank 列表 失败:');
    } catch (e) {
      print('getIntegralRank 列表 失败: $e');
      throw Exception(e.toString());
      // return null;
    }
  }

  // 获取用户积分记录
  static Future<Map<String, dynamic>?> taskProgress({required int code}) async {
    try {
      Map<String, dynamic> params = {'task_code': code};
      var resp = await HttpManager().post(API.taskProgress, params);
      print('taskProgress DATA: ${resp!.data}');
      if (resp.code == HttpConfig().successCode) {
        return resp.data;
      }
      return {"completed": 0, "task_rules": []};
    } catch (e) {
      print('获取累加类型任务进度 失败: $e');
      throw Exception(e.toString());
    }
  }
  static Future<bool?> isAndroidGroupExist() async{
    try{
      var resp = await HttpManager().get(API.getAndroidGroupChatExist+'?room_id=83', {});
      print('isAndroidGroupExist ${resp!.data}');
      if(resp!.code == '1409'){
        return false;
      }
      final status =  resp.data!['status']?.toString();
      if (resp.code == HttpConfig().successCode ) {
        if(status == '2'){
          // 群不存在
          return false;
        }
      }else{
        return null;
      }
      // 群已存在
      return true;
    }catch(e){
      print('isAndroidGroupExist $e');
      return null;
    }
  }
}
